package Moudle;

import org.bson.Document;

public class OutItem {
    /**
     * this.url不是指向文章详情页的链接
     * 是在搜索页上展示的链接
     * this._id 是 this.url中解析出相关信息
     * url前缀：https://kns.cnki.net/kcms/detail/detail.aspx?
     * 前缀 + this._id ：  即为指向该文章的链接
     */
    public String _id, url, select_title, select_author, select_学位授予单位, select_degree, select_get_degree_year,
            select_quote_count, select_quote_url, select_download_count, dbcode, dbname, filename;

    public Document toDocument(){
        /**
         * 转换成bson.Document
         */
        dbcode = get_dbcode();
        dbname = get_dbname();
        filename = get_filename();
        _id = get_id();

        return new Document("_id", _id)
                .append("dbcode", dbcode)
                .append("dbname", dbname)
                .append("filename", filename)
                .append("select_url", url)
                .append("select_title", select_title)
                .append("select_author", select_author)
                .append("select_学位授予单位", select_学位授予单位)
                .append("select_degree", select_degree)
                .append("select_get_degree_year", select_get_degree_year)
                .append("select_quote_count", select_quote_count)
                .append("select_quote_url", select_quote_url)
                .append("select_download_count", select_download_count);
    }

    public String get_id(){
        /**
         * url前缀：https://kns.cnki.net/kcms/detail/detail.aspx?
         * url前缀 + id
         * 即为该文章的详情页链接
         */
        return "dbcode="+dbcode+"&dbname="+dbname+"&filename="+filename;
    }

    public String get_dbcode(){
        int leftIndex = this.url.indexOf("dbcode=")+7;
        int rightIndex = this.url.indexOf("&QueryID");
        return this.url.substring(leftIndex, rightIndex);
    }

    public String get_dbname(){
        int leftIndex = this.url.indexOf("dbname=") + 7;
        int rightIndex = this.url.indexOf("&filename");
        return this.url.substring(leftIndex, rightIndex);
    }

    public String get_filename(){
        int leftIndex = this.url.indexOf("filename=") + 9;
        return this.url.substring(leftIndex);
    }

    public String get_select_title(String htmlText){
        return htmlText.replaceAll("document.write\\(ReplaceChar1\\(ReplaceChar\\('|'\\)\\)\\);", "");
    }
}
